package com.alibaba.citrus.cr.mn.order.center.facade.service.impl;

import com.alibaba.citrus.cr.mn.order.center.facade.service.OrderBizDomainService;
import com.alibaba.citrus.cr.mn.order.center.forecastorder.ForecastOrderBizDomainService;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.mnordercenter.forecastorderquery.dto.QuerySingleRequest;
import com.epoch.app.mnordercenter.forecastorderquery.service.ForecastOrderQueryService;
import com.epoch.app.mnordercenter.model.dto.ForecastOrderBizQueryResponse;
import com.epoch.app.mnordercenter.model.dto.OrderBizQueryResponse;
import com.epoch.app.mnordercenter.model.dto.RelationOrderSDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhoubc
 * @date 2023/6/1 15:11
 */
@Primary
@Component("forecastOrderQueryServiceCenter")
public class ForecastOrderQueryServiceImpl implements ForecastOrderQueryService {

    @Resource
    private OrderBizDomainService orderBizDomainService;
    @Resource
    private ForecastOrderBizDomainService forecastOrderBizDomainService;

    @Override
    public Result<ForecastOrderBizQueryResponse> querySingle(QuerySingleRequest querySingleRequest) {
        Result<ForecastOrderBizQueryResponse> forecastOrderResponse = forecastOrderBizDomainService.queryForecastOrderDetail(querySingleRequest.getId());
        ForecastOrderBizQueryResponse forecastOrderBizQueryResponse = forecastOrderResponse.getResult();
        if(StringUtils.isNotBlank(forecastOrderBizQueryResponse.getId())){
            Result<List<OrderBizQueryResponse>> orderResult = orderBizDomainService.queryOrderByFrontOrderId(forecastOrderBizQueryResponse.getId());
            List<RelationOrderSDO> relationOrderList = new ArrayList<>();
            for (OrderBizQueryResponse sdo : orderResult.getResult()) {
                RelationOrderSDO relationOrderSDO = new RelationOrderSDO();
                relationOrderSDO.setId(sdo.getId());
                relationOrderSDO.setOrderBizId(sdo.getOrderBizId());
                relationOrderSDO.setOrderType(sdo.getOrderTypeName());
                relationOrderSDO.setOrderTime(sdo.getOrderTime());
                relationOrderSDO.setCreator(sdo.getCreator());
                relationOrderSDO.setRemark(sdo.getRemark());
                relationOrderList.add(relationOrderSDO);
            }
            forecastOrderBizQueryResponse.setOrderList(relationOrderList);
        }
        return Result.success(forecastOrderBizQueryResponse);
    }

}
